#include "system_tick.h"
#include "led_sw.h"
#include "beep.h"
#include "rf_receive.h"

// Variables
volatile bit v_RfIn;
volatile bit v_RfOld;

volatile U16 v_TimeLow = 0;
volatile U16 v_TimeHigh = 0;
U8 s_BitCnt = 0;
U8 s_pRFBuff[3];
bit s_ReceiveFlag = 0;
U8 s_RFSysnBitTimeout = TIMEOUT_RF;

U8 s_pRFIncome[3];
//eeprom U8 e_pRFIncome[3];
U8 s_BitCode;

eeprom U8 e_pRFCode[5][3] = {{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}};
eeprom U8 e_pRFCodeCnt = 0;

U8 s_pRFCode[5][3];
U8 s_pRFCodeCnt;

void interrupt isr_rt(void);

void RF_InitReset(void)
{
    U8 i;

    // init for RF receiver
    RF_TRIS = 1;
    RF_PULLUP = 0;
    v_RfIn = RF;
    GPIF = 0;
    RF_INT_ANYCHANGE = 1;
    GPIE = 1;
    v_RfOld = v_RfIn;

    // init timer1 for 65,536ms interrupt
    //timer1 se dem 1us 1 lan, bo dem 16 bit
    TMR1L = 0;
    TMR1H = 0;
    TMR1IF = 0;
    T1CON = 0x14;
    TMR1ON = 0;

    eecpymem(&s_pRFCodeCnt, &e_pRFCodeCnt, 1);
    if( 5 < s_pRFCodeCnt )
    {
        RF_EraseAllRFCode();
    }
    else
    {
        for ( i=0; i<s_pRFCodeCnt; i++ )
        {
            eecpymem(s_pRFCode[i], e_pRFCode[i], 3);
        }
    }
}

void RF_EraseAllRFCode (void)
{
    s_pRFCodeCnt = 0;
    memcpyee(&e_pRFCodeCnt, &s_pRFCodeCnt, 1);
}

void RF_SaveAllRFCode(void)
{
    U8 i;
    memcpyee(&e_pRFCodeCnt, &s_pRFCodeCnt, 1);
    for ( i=0; i<s_pRFCodeCnt; i++ )
    {
        memcpyee(e_pRFCode[i], s_pRFCode[i], 3);
    }
}

bool RF_IsExist(void)
{
    U8 i;
    for (i=0; i<s_pRFCodeCnt; i++)
    {
        if( s_pRFIncome[0] == s_pRFCode[i][0] && s_pRFIncome[1] == s_pRFCode[i][1] && s_pRFIncome[2] == s_pRFCode[i][2] )
        {
            return (true);
        }
    }
    return (false);
}

void RF_GetReceiveCode(void)
{
    s_pRFIncome[0] = s_pRFBuff[0];
    s_pRFIncome[1] = s_pRFBuff[1];
    s_pRFIncome[2] = s_pRFBuff[2] & BIT_CODE_FILTER;
    s_BitCode  = s_pRFBuff[2] & BIT_CODE_MARK;
}

bool RF_receive( void )
{
    U16 timeHigh;
    U16 timeLow;
    U8 byteIndex;
    U8 temp;
    
    if (0 == s_ReceiveFlag) return false;
    s_ReceiveFlag = 0;
    
    timeHigh = v_TimeHigh;
    timeLow = v_TimeLow;
    
    if ((timeHigh > timeLow) && ((timeHigh>>3) < timeLow))
    {
        byteIndex = (s_BitCnt>>3) & 0x03;
        temp = s_pRFBuff[byteIndex]<<1;
        s_pRFBuff[byteIndex] = temp | 1;
    }
    else if( (timeLow > timeHigh) && ( (timeLow>>3) < timeHigh ) )
    {
        byteIndex = (s_BitCnt>>3) & 0x03;
        temp = s_pRFBuff[byteIndex]<<1;
        s_pRFBuff[byteIndex] = temp;
    }
    else
    {
        s_BitCnt = 0;
        return false;
    }
    
    s_BitCnt++;
    if ((NUMBER_RF_BIT*2) <= s_BitCnt)
    {
        stop_timer1();
        s_BitCnt = 0;
        if(s_RFSysnBitTimeout >= TIMEOUT_RF)
        {
            s_RFSysnBitTimeout = 0;
            return true;
        }
        s_RFSysnBitTimeout = 0;
    }
    return false;
}


void interrupt isr_rt(void)
{
    if(1 == GPIF)
    {
        v_RfIn = RF;
        v_SsIn = SS;
        GPIF = 0;
        if( v_RfIn != v_RfOld )
        {
            stop_timer1();
            if(v_RfIn)
            {
                v_TimeLow = get_timer1();
                s_ReceiveFlag = 1;
            }
            else
            {
                v_TimeHigh = get_timer1();
            }
            start_timer1();
            v_RfOld = v_RfIn;
        }
        if(v_SsIn != v_SsOld)
        {
            v_SsOld = v_SsIn;
            // if(SS_ACTIVE == v_SsIn)
            // {
                // v_SsActive = true;
            // }
            v_SsActive = true;
        }
    }

    if(1 == TMR1IF)
    {
        TMR1IF = 0;
        stop_timer1();
    }

    #ifdef _12F683
    if(1 == T0IF)
    {
        T0IF = 0;
        TMR0 = TIMER0_RELOAD;
        SystemtickFlag = true;
        if(s_RFSysnBitTimeout < TIMEOUT_RF) s_RFSysnBitTimeout++;
    }
    
    if(1 == TMR2IE && 1 == TMR2IF)
    {
        TMR2IF = 0;
        if( INC_TYPE == Beep_GetPulseType() )
        {
            Beep_IncPeriod();
            if( PWM_ALARM_MAX_PR2 == Beep_GetPeriod() )
            {
                Beep_SetPulseType(DEC_TYPE);
            }
        }
        else if( DEC_TYPE == Beep_GetPulseType() )
        {
            Beep_DecPeriod();
            if( PWM_ALARM_MIN_PR2 == Beep_GetPeriod() )
            {
                Beep_SetPulseType(INC_TYPE);
            }
        }
    }
    #endif
}

